import storage from 'good-storage'

const LOGIN_NAME = 'LOGIN_NAME'
const ACCESS_TOKEN = 'access_token'
const CURRENT_USER = 'CURRENT_USER'
const LAST_LOGIN_TIME = 'LAST_LOGIN_TIME'

export function saveAccessToken(accessToken) {
  storage.session.set(ACCESS_TOKEN, {accessToken})
}

export function getAccessToken() {
  let accessToken = storage.session.get(ACCESS_TOKEN, null)
  return (accessToken && accessToken.accessToken) || accessToken
}

export function saveCurrentUser(currentUser) {
  storage.session.set(CURRENT_USER, currentUser)
}

export function getCurrentUser(defaultValue = {
  id: null,
  name: null,
  user_token: null
}) {
  return storage.session.get(CURRENT_USER, defaultValue)
}

export function saveLoginName(loginName) {
  storage.session.set(LOGIN_NAME, loginName)
}

export function getLoginName(loginName = null) {
  return storage.session.get(LOGIN_NAME, loginName)
}

export function saveLastLoginTime(lastLoginTime) {
  storage.session.set(LAST_LOGIN_TIME, lastLoginTime)
}

export function getLastLoginTime(lastLoginTime = null) {
  return storage.session.get(LAST_LOGIN_TIME, lastLoginTime)
}

export function clear() {
  storage.session.clear()
}

export function _clearLocal() {
  storage.session.clear()
}
